package com.atguigu.ggkt.activity.service.impl;


import com.atguigu.ggkt.activity.mapper.CouponInfoMapper;
import com.atguigu.ggkt.activity.service.CouponInfoService;
import com.atguigu.ggkt.activity.service.CouponUseService;
import com.atguigu.ggkt.client.user.UserInfoFeignClient;
import com.atguigu.ggkt.model.activity.CouponInfo;
import com.atguigu.ggkt.model.activity.CouponUse;
import com.atguigu.ggkt.model.user.UserInfo;
import com.atguigu.ggkt.vo.activity.CouponUseQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 优惠券信息 服务实现类
 * </p>
 *
 * @author sleephardzz
 * @since 2023-07-23
 */
@Service
public class CouponInfoServiceImpl extends ServiceImpl<CouponInfoMapper, CouponInfo> implements CouponInfoService {
    @Autowired
    private CouponUseService couponUseService;
    //远程调用接口
    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    //更新优惠卷
    @Override
    public void updateCouponInfoUseStatus(Long couponUseId, Long orderId) {
        CouponUse couponUse = new CouponUse();

    }
    //重要方法：获取已经使用过的优惠卷列表
    @Override
    public IPage<CouponUse> selectCouponUsePage(Page<CouponUse> pageParam, CouponUseQueryVo couponUseQueryVo) {
        LambdaQueryWrapper<CouponUse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!StringUtils.isEmpty(couponUseQueryVo.getCouponId()),CouponUse::getCouponId , couponUseQueryVo.getCouponId());
        wrapper.eq(!StringUtils.isEmpty(couponUseQueryVo.getCouponStatus()),CouponUse::getCouponStatus , couponUseQueryVo.getCouponStatus());
        //大于等于
        wrapper.ge(!StringUtils.isEmpty(couponUseQueryVo.getGetTimeBegin()),CouponUse::getGetTime , couponUseQueryVo.getGetTimeBegin());
        //小于等于
        wrapper.le(!StringUtils.isEmpty(couponUseQueryVo.getGetTimeEnd()),CouponUse::getGetTime , couponUseQueryVo.getGetTimeEnd());
        //调用方法查询
        IPage<CouponUse> page = couponUseService.page(pageParam, wrapper);
        //封装用户昵称和手机号
        List<CouponUse> couponUseList = page.getRecords();
        couponUseList.stream().forEach(item->{
            this.getUserInfoByCouponUseId(item);
        });
        return page;
    }


    private void getUserInfoByCouponUseId(CouponUse couponUse) {
        Long userId = couponUse.getUserId();
        if (!StringUtils.isEmpty(userId)){
            //远程调用
            UserInfo userInfo = userInfoFeignClient.getById(userId);
            if (userInfo!=null){
                couponUse.getParam().put("nickName", userInfo.getNickName());//获得昵称
                couponUse.getParam().put("phone", userInfo.getPhone());//获得手机号
            }
        }
    }
}
